/***************************************************************************
 *  Filename: remotecomputer.h
 *  Class: RemoteComputer
 *
 *  Copyright 2012  Xander van Kooten
 *  <xandervk@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of Parsax-Remote-Controller.
 *
 *    Parsax-Remote-Controller is free software: you can redistribute it
 *    and/or modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    Parsax-Remote-Controller is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Parsax-Remote-Controller.
 *    If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef REMOTECOMPUTER_H
#define REMOTECOMPUTER_H

#include <QObject>
#include "mastercommunicator.h"
#include "messageparser.h"
#include "remoteprocess.h"
/*!
 \brief Represents a slave computer.

 The RemoteComputer class handles the logic and communications with the
 slave. It has a MessageParser, a MasterCommunicator and a list of RemoteProcess.

 This allows to use the RemoteComputer to monitor a slave with multiple processes.

 \todo Handle setting messages.
 \todo Handle settings from within the class.
*/
class RemoteComputer : public QObject
{
    Q_OBJECT
public:
    /*!
     \brief Object Constructor.

     \param parent
    */
    explicit RemoteComputer(QObject *parent = 0);

    /*!
     \brief Set the computer name.

     \param tname The name that should be given to the computer.
    */
    void setName(QString tname);
    /*!
     \brief Set the slave's address to connect to.

     \param taddress The address to the slave.
    */
    void setAddress(QString taddress);
    /*!
     \brief Set the port for connection.

     \param tport Port to connect to.
    */
    void setPort(int tport);

    /*!
     \brief Return the name given.

     \return QString
    */
    QString getName();
    /*!
     \brief Return the slave address (according to the remote process)

     \return QString
    */
    QString getAddress();
    /*!
     \brief Return the connection port.

     \return int
    */
    int getPort();

    /*!
     \brief Add an empty process.

     \deprecated Use only the expanded version.
    */
    void addProccess();
    /*!
     \brief Add a new process to the computer.

     \param id The id of the new process.
     \param program The program itself.
     \param parameters Parameters to start the program.
     \param timeout Monitoring timeout. (For future use).
    */
    void addProccess(QString id, QString program, QStringList parameters, int timeout);
    /*!
     \brief Add an existing process to the computer.

     \param newProc The process to be added.

     Besided adding the process to the computer's processes list, the function setPc() from the
     RemoteProcess is called.
    */
    void addProccess(RemoteProcess *newProc);

    /*!
     \brief Returns the state of the connection.

     \return bool True if connected of false if not.
    */
    bool isConnected();
signals:
    /*!
     \brief Emmited when a connection is established.

    */
    void connectionEstablished();
    /*!
     \brief Emmited when connection is lost.

    */
    void connectionLost();
public slots:
    /*!
     \brief Establish a connection with the slave.

    */
    void connectToSlave();
    /*!
     \brief Disconnect from the slave.

    */
    void disconnectFromSlave();
private:
    QString name; /*!< TODO */
    QString address; /*!< TODO */
    int port; /*!< TODO */
    bool connected; /*!< TODO */
    MasterCommunicator communicationHandler; /*!< TODO */
    MessageParser parser; /*!< TODO */
    QMap<QString, RemoteProcess*> processes; /*!< TODO */

private slots:
    /*!
     \brief Handle disconnections.

     Updates the status of all the processes to "notconnected".
    */
    void whenDisconnected();
    /*!
     \brief Handles connection.

     Updates the status of all the processes to "nostatus".
    */
    void whenConnected();
    /*!
     \brief Try to start a remote process.

     \param id The process id.
    */
    void whenProcessShouldStart(QString id);
    /*!
     \brief Try to stop a remote process.

     \param id The process id.
    */
    void whenProcessShouldStop(QString id);
    /*!
     \brief Handles the reception of a status update.

     \param id Id of the process which status should be updated.
     \param status The status.
    */
    void whenStatusUpdateReceived(QString id, QString status);
    /*!
     \brief Handles the reception of CLO messages.

     \param id Id of the process.
     \param type The type of the message (normal=0, error=1).
     \param message The received message.
    */
    void whenCLOReceived(QString id, int type, QString message);
};

#endif // REMOTECOMPUTER_H
